import sys
import ast
import os
import re

# 将工程目录加入包扫描
currPath = os.path.abspath(os.path.dirname(__file__))
srcPath = os.path.split(currPath)[0]
rootPath = os.path.split(srcPath)[0]
projectPath = os.path.split(rootPath)[0]
sys.path.append(projectPath)

from typing import List
from pandas import Series,DataFrame
from elasticsearch import Elasticsearch
from tqdm import tqdm

from src.base import Common, Consts
from src.base.Report import Report
from src.db.CcxDataCenterDb import CcxDataCenter
from src.db.FCDB import FCDB
from src.db.ProdDb import ProdDb
from src.tasks.enterpriseToEs.EntEnterprise import EntEnterprise
from src.tasks.enterpriseToEs.RegionsData import RegionsData
from src.tasks.enterpriseToEs.SyncEnterpriseNameList import SyncEnterpriseNameList
from src.tasks.enterpriseToEs.AbsDataSpread import AbsDataSpread
from src.utils.LogUtils import Logger
from src.db import AbsSqlServer
from src.db.AbsSqlServer import AbsSqlServerDev

es = Elasticsearch(
    [{'host': Consts.elasticsearch_url, 'port': Consts.elasticsearch_port}], timeout=3600
)


def deleteEntList():
    es.indices.delete(index=Consts.es_enterpriseList_index, ignore=400)

def writeMainEntList(enterpriseList: List[EntEnterprise]):
    es.indices.delete(index=Consts.es_enterpriseList_index, ignore=400)
    es.indices.create(index=Consts.es_enterpriseList_index, ignore=400)
    for item in tqdm(enterpriseList, desc="es数据库保存/更新中...", position=0):
        item = dict(item)
        body = {
            "query": {
                "bool": {
                    "must": [
                        {
                            "term": {
                                "credit_code.keyword": item.get("credit_code")
                            }
                        },
                        {
                            "term": {
                                "year": item.get("year")
                            }
                        }
                    ]
                }
            },
            "track_total_hits": True
        }
        if item.get("year") is None:
            body = {
                "query": {
                    "bool": {
                        "must": [
                            {
                                "term": {
                                    "credit_code.keyword": item.get("credit_code")
                                }
                            }
                        ]
                    }
                },
                "track_total_hits": True
            }
        queryResult = es.search(index=Consts.es_enterpriseList_index, body=body)
        if len(queryResult['hits']['hits']) > 0:
            for updateItem in queryResult['hits']['hits']:
                es.index(index=Consts.es_enterpriseList_index, doc_type='test-type', id=updateItem['_id'],
                         body=item)
        else:
            es.index(index=Consts.es_enterpriseList_index, doc_type='test-type', body=item)

def main(logger, localDb, dataCenterDb, fcdb, useCache):
    absSqlServer = AbsSqlServerDev()
    # deleteEntList()
    # 1: 同步主体名单
    nameList = SyncEnterpriseNameList(logger=logger, localDb=localDb, dataCenterDb=dataCenterDb, fcdb=fcdb, useCache=useCache)
    absInfo = AbsDataSpread(logger=logger,  absSqlServer=absSqlServer)

    entList = nameList.__queryDataOtherForEs__()
    deb = nameList.__queryDebentureBase_V2__()
    deb_dict = {key: value for key, value in deb}
    my_list = []
    credits = []
    comps = []
    for x in entList:
        print(x['id'])
        comps.append(x['comp_code'])
        credits.append(x['credit_code'])
        all_bp = absInfo.__queryBondsRateSpreadByEnt__(x['credit_code'])
        if len(all_bp) == 1:
            x['all_bp'] = all_bp[0]['all_bp_weight_value']

        my_list = my_list + nameList.__queryReportT001DataByYear__(x)

    print('开始')
    tcr0033s = nameList.__queryTCR0033_V2__(comps)
    dfltinfos = nameList.__queryDfltinfo_V2__(comps);
    nonStandards = nameList.__queryBaseNonStandard_V2__(credits)
    listeds = nameList.__queryListed_V2__(credits)
    qeChanges = nameList.__queryQeChange_V2__(credits)
    externalChanges = nameList.__queryExternalChange_V2__(credits)
    accbilloddetails = nameList.__queryAccbilloddetail_V2__(credits)

    for x in my_list:
        if x['comp_code'] is not None:
            if str(tcr0033s).__contains__(x['comp_code']):
                x['tcr0033'] = 'TRUE'
            else:
                x['tcr0033'] = 'FALSE'
        else:
            x['tcr0033'] = 'FALSE'
        if any(item["credit_code"] == x['credit_code'] for item in nonStandards):
            x['nonStandard'] = 'TRUE'
        else:
            x['nonStandard'] = 'FALSE'

        if x['comp_code'] is not None:
            if str(dfltinfos).__contains__(x['comp_code']):
                x['dfltinfo'] = 'TRUE'
                x['dfltinoRegion'] = 'TRUE'
            else:
                x['dfltinfo'] = 'FALSE'
                x['dfltinoRegion'] = 'FALSE'
        else:
            x['dfltinfo'] = 'FALSE'
            x['dfltinoRegion'] = 'FALSE'

        if any(item["credit_code"] == x['credit_code'] for item in listeds):
        # if str(listeds[0]['credit_code']).split(",").__contains__(x['credit_code']):
            x['listed'] = 'TRUE'
        else:
            x['listed'] = 'FALSE'

        # if str(qeChanges[0][0]).split(",").__contains__(x['credit_code']):
        if str(qeChanges).__contains__(x['credit_code']):
            x['qeChange'] = 'TRUE'
        else:
            x['qeChange'] = 'FALSE'

        if str(externalChanges).__contains__(x['credit_code']):
        # if str(externalChanges[0][0]).split(",").__contains__(x['credit_code']):
            x['externalChange'] = 'TRUE'
        else:
            x['externalChange'] = 'FALSE'

        if str(accbilloddetails).__contains__(x['credit_code']):
        # if str(accbilloddetails[0][0]).split(",").__contains__(x['credit_code']):
            x['accbilloddetail'] = 'TRUE'
        else:
            x['accbilloddetail'] = 'FALSE'

        if deb_dict.__contains__(x['credit_code']):
            x['debSum'] = deb_dict[x['credit_code']]

    # 写入es
    writeMainEntList(my_list)
    print('产业/金融数据es写入完成')

# 程序入口
if __name__ == "__main__":
    logger = Logger()
    localDb = ProdDb(logSql=False, autocommit=True)
    dataCenterDb = CcxDataCenter(logSql=False, autocommit=True)
    fcdb = FCDB(logSql=False, autocommit=True)
    useCache = False
    main(logger, localDb, dataCenterDb, fcdb, useCache)
